<template>
  <div>
    <el-dialog
      title="修改地址"
      :visible.sync="locationCascaderState"
      @close="dialogClosed"
    >
      <el-form
        label-position="top"
        :rules="cityFormRules"
        :model="addressForm"
        ref="addressFormRef"
      >
        <el-form-item label="省市区/县" prop="city">
          <el-cascader
            :options="cityData"
            :props="cascaderProps"
            v-model="addressForm.selectedCity"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model.trim="addressForm.address"></el-input>
        </el-form-item>
      </el-form>

      <template slot="footer">
        <el-button>取消</el-button>
        <el-button type="primary">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import cityData from '@/components/order/city_data2017_element'
export default {
  name: 'LocationCascader',
  props: ['id'],
  data() {
    const checkCity = (rule, val, cb) => {
      if (this.selectedKeys.length === 0) {
        return cb(new Error('请选择城市！'))
      }
      cb()
    }
    return {
      cascaderProps: {
        expandTrigger: 'hover',
        label: 'label',
        value: 'value',
        children: 'children'
      },
      addressForm: {
        selectedCity: [],
        address: ''
      },

      cityFormRules: {
        city: [{ validator: checkCity, trigger: 'blur' }],
        address: [
          { required: true, message: '请填写详细地址', trigger: 'blur' }
        ]
      },
      cityData
    }
  },
  computed: {
    locationCascaderState: {
      set() {
        this.$store.commit('order/locationCascaderState', false)
      },
      get() {
        return this.$store.state.order.locationCascader
      }
    }
  },
  methods: {
    dialogClosed() {
      this.$store.commit('order/locationCascaderState', false)
      this.$refs.addressFormRef.resetFields()
      this.addressForm.selectedCity = []
    }
  }
}
</script>

<style lane="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
